package com.zjs.service.impl;

import com.zjs.dao.BookDao;
import com.zjs.pojo.Book;
import com.zjs.service.BookService;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;
import pojo.BookVo;
import util.ResultStatus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BookServiceimpl implements BookService {
    @Autowired
    private BookDao bookdao;
    @Autowired
    ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Override
    public ResultStatus EsList(BookVo bookvo) {
        if (bookvo.getPageNum()<1 ||bookvo.getPageNum()==null || bookvo.getPageNum().equals("")){
            bookvo.setPageNum(0);
        } else{
            bookvo.setPageNum(bookvo.getPageNum()-1);
        }

        //分页
        PageRequest of = PageRequest.of(bookvo.getPageNum(), bookvo.getPageSize());
        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
        //高亮
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        highlightBuilder.preTags("<font color='red'>").postTags("</font>").field("bookName");
        //排序
        FieldSortBuilder order =null;
        if (bookvo.getPx()==1 && bookvo.getPx()!=null && !bookvo.getPx().equals("")){
            System.err.println("收藏排名");
            order = new FieldSortBuilder("realPay").order(SortOrder.ASC);
        }
        if (bookvo.getPx()==2 && bookvo.getPx()!=null && !bookvo.getPx().equals("")){
            System.err.println("点赞排名");
            order = new FieldSortBuilder("realPay").order(SortOrder.ASC);
        }

        NativeSearchQuery query=null;
        if (bookvo.getName()==null || bookvo.getName().equals("")){
            query=builder.withPageable(of)
                    .withSort(order)
                    .build();
        } else{
            query=builder.withPageable(of)
                    .withQuery(QueryBuilders.multiMatchQuery(bookvo.getName(),"bookName"))
                    .withHighlightBuilder(highlightBuilder)
                    .withSort(order)
                    .build();
        }
        long count = elasticsearchRestTemplate.count(query, Book.class);
        Integer pageNum=bookvo.getPageNum()+1;
        Integer pageSize=bookvo.getPageSize();
        long total=0;
        if (count%pageSize>0){
            total=count/pageSize+1;
        }else{
            total=count/pageSize;
        }
        Map<String, Object> map = new HashMap<>();
        ArrayList<Book> list = new ArrayList<>();
        if (count==0){

        }else{
            SearchHits<Book> search = elasticsearchRestTemplate.search(query, Book.class);
            search.forEach(userSearchHit -> {
                Book content = userSearchHit.getContent();
                List<String> bookName = userSearchHit.getHighlightField("bookName");
                System.err.println(bookName);
                if (bookName!=null && bookName.size()>0){
                    content.setBookName(bookName.get(0));
                }
                list.add(content);
            });
        }
        map.put("list",list);
        map.put("pageNum",pageNum);
        map.put("pageSize",pageSize);
        map.put("count",count);
        map.put("total",total);

        return ResultStatus.SUCCESS(map);
    }
}
